#-------------------------------------------------------------------
# Project Invasion of the aliens 
# name game.py
# purpose main game loop
#
# authors Chris L'Heureux, Tyler Plante, Jackson Morse
# main author Chris L'Heureux,
#
# created 12/3/12
# Copyright (C) Chris L'Heureux,
# License New BSD\
#------------------------------------------------------------------  
import pygame, math, sys, random

class Player():
    def __init__(self, speed, position):
        self.baseSurfaces = []
        self.baseSurfaces += [pygame.image.load("images/Player/player.png")]
        self.frame = 0
        self.maxFrame = len(self.baseSurfaces)-1
        self.surface = self.baseSurfaces[self.frame]
        self.rect = self.surface.get_rect()
        self.baseRect = self.rect
        self.radius = (self.rect.width/2 + self.rect.height/2)/2
        self.angle = 0
        self.speed = 0
        self.maxSpeed = speed 
        self.rotSpeed = 0 
        self.maxRotSpeed = 10
        self.place(position)
        self.living = False
        self.dir = "none"
        self.turning = "none"

        self.x = self.rect.center[0]
        self.y = self.rect.center[1]
        
        if pygame.mixer:
            self.bounceSound = pygame.mixer.Sound("")
            
                    
        
    def  __str__(self):
        return ("I'm the Player and i am LIVING " + str(self.rect.center) + str(self.speed) + str(self.living))
     
    def die(self):
        self.living = False
     
    def place(self, position):
        self.rect.center = position
        
    def newPos(self):
        #print ">>>>>>>>>>>>>>>>>",self.angle,"<<<<<<<<<<<<<<<<<<<"
        self.angle += self.rotSpeed
        self.angle = self.angle % 360
        if self.angle < 0:
            self.angle = 360 - math.abs(self.angle)
        if self.angle == 0:
            self.x += self.speed
            self.y += 0
        elif 0 < self.angle < 90: 
            self.x += self.speed * math.cos(math.radians(self.angle))
            self.y += -self.speed * math.sin(math.radians(self.angle))
        elif self.angle == 90:
            self.x += 0
            self.y += -self.speed
        elif 90 < self.angle < 180:
            self.x += -self.speed * math.sin(math.radians(self.angle-90))
            self.y += -self.speed * math.cos(math.radians(self.angle-90))
        elif self.angle == 180:
            self.x += -self.speed
            self.y += 0
        elif 180 < self.angle < 270:
            self.x += -self.speed * math.cos(math.radians(self.angle-180))
            self.y += self.speed * math.sin(math.radians(self.angle-180))
        elif self.angle == 270:
            self.x += 0
            self.y += self.speed 
        elif 270 < self.angle < 360:
            self.x += self.speed * math.sin(math.radians(self.angle-270))
            self.y += self.speed * math.cos(math.radians(self.angle-270))

        
    def move(self):        
        self.newPos()
        #print "I've moved", self.speed, self.dir, self.angle
        self.rect.center = [int(self.x), int(self.y)]
        self.surface = pygame.transform.rotate(
                                self.baseSurfaces[self.frame], 
                                self.angle)
        self._clip()
                                
    def _clip(self):
        self.rect = self.surface.get_rect(center=self.rect.center)
        clipped_rect = pygame.Rect(
                (self.rect.width - self.baseRect.width) / 2,
                (self.rect.height - self.baseRect.height) / 2,
                self.baseRect.width,
                self.baseRect.height,
            )
        self.surface = self.surface.subsurface(clipped_rect)
        self.rect = self.surface.get_rect(center=self.rect.center)
        
    def retry(self, retry, pos):
        if retry == "yes":
            self.living = True
            
            self.angle = 0
            self.speed = 0
            self.rotSpeed = 0 
            self.place(pos)
            self.living = True
            
            self.dir = "none"
            self.turning = "none"

            self.x = self.rect.center[0]
            self.y = self.rect.center[1]
    
    
    def direction(self, dir):
        #return ("where to go")
        if dir == "forward":
            self.speed = self.maxSpeed
            self.dir = "forward"
        elif dir == "stop":
            self.speed = 0
            self.dir = "stop"
        elif dir == "clockwise":
            self.rotSpeed = -self.maxRotSpeed
            self.turning = "clockwise"
        elif dir == "stop clockwise" and self.turning == "clockwise":
            self.rotSpeed = 0
            self.turning = "stop clockwise"
        elif dir == "counter clockwise":
            self.rotSpeed = self.maxRotSpeed
            self.turning = "counter clockwise"
        elif dir == "stop counter clockwise" and self.turning == "counter clockwise":
            self.rotSpeed = 0
            self.turning = "stop counter clockwise"
            
    def DistanceToPoint():
        x1 = self.rect.center[0]
        x2 = pt[0]
        y1 = self.rect.center[1]
        y2 = pt[1]
        return math.sqrt(((x2-x1)**2)+((y2-y1)**2))\
        
    def wallCollide(self, screenWidth, screenHeight):
        if (self.rect.left < 0 or self.rect.right > screenWidth):
                self.die()
        if (self.rect.top < 0 or self.rect.top > screenHeight):
                self.die()
				
                
	def collideBullet(self, other):
		if (self.rect.right > other.rect.left 
			and self.rect.left < other.rect.right):
			if (self.rect.bottom > other.rect.top and 
				self.rect.top < other.rect.bottom):
				if (self.distToPoint(other.rect.center)
					< self.radius + other.radius):  
					if self.health <= 0:
						self.living = False            
        
                					
        #print "trying to hit ", str(other)
        
        
    #TEST    
    
    